<?php
namespace app\question\controller\admin;
use app\common\controller\Admin;
use app\common\annotation\NodeAnnotation;
use app\common\traits\ControllerCurd;
use app\question\model\QuestionExamPaper;
use app\question\model\QuestionSubject;
use app\question\model\QuestionTestPaper;
use think\Request;

/**
 * @NodeAnnotation(
 *     title="试卷模块",
 *     parent="question/admin.index",
 *     is_menu=0,
 *     is_view=0,
 *     auth=1,
 * )
 */
class Testpaper extends Admin
{
    use ControllerCurd;
    public $model;
    public function initialize()
    {
        parent::initialize();

        $this->assign([
            "examId" => $this->request->param("exam_id",0,'intval')
        ]);
        $this->model = new QuestionTestPaper();
    }

    /**
     * @NodeAnnotation(
     *     title="试卷列表",
     *     parent="question/admin.testpaper",
     *     MenuRoot="question/admin.index"
     * )
     */
    public function index($exam_id=0)
    {
        $where = [];
        if(!empty($exam_id))
        {
            $id = QuestionExamPaper::where('exam_id',$exam_id)->column('testpaper_id');
            array_push($where,['id','in',$id]);
        }
        $this->assign([
            "testPaperList" => $this->model->where($where)->paginate()
        ]);
        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="添加试卷",
     *     parent="question/admin.testpaper",
     *     MenuRoot="question/admin.index",
     *     is_menu = 0
     * )
     */
    public function addtestpaper(Request $request)
    {
        if($request->isAjax())
        {
            $input = $request->post();
            $res = $this->model->create($input);
            if($res)
            {
                $jumpParam =[];
                if(isset($input['exam_id']) and !empty($input['exam_id']))
                {
                    QuestionExamPaper::create([
                        'testpaper_id' => $res['id'],
                        'exam_id'      => $input['exam_id'],
                    ]);
                    $jumpParam['exam_id'] = $input['exam_id'];
                }
                $this->success('添加成功',url('question/admin.testpaper/index',$jumpParam));
            }else{
                $this->error('添加失败');
            }
        }
        $this->assign([
            'subjectList' => QuestionSubject::select()
        ]);
        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="编辑试卷",
     *     parent="question/admin.testpaper",
     *     MenuRoot="question/admin.index",
     *     is_menu = 0
     * )
     */
    public function edittestpaper(Request $request,$id=0)
    {
        $this->assign([
            "testpaper" =>  $this->model->findData($id),
            'subjectList' => QuestionSubject::select()
        ]);
        $this->edit();
        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="删除试卷",
     *     parent="question/admin.testpaper",
     *     MenuRoot="question/admin.index",
     *     is_menu = 0
     * )
     */
    public function deltestpaper(Request $request)
    {
        $this->del('',function($mode,$data){
            QuestionExamPaper::whereIn("testpaper_id",$data)
                ->where("exam_id",$this->request->param("exam_id",0,'intval'))
                ->delete();
        });
    }
}